package com.penton.util.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

/**
 * 解决ListView与Gallery冲突问题<br/>
 * 还需要设置listView.setFocusableInTouchMode(false);
 * @author pengjianbo <pengjianbo@haodou.com>
 * $Id: BetterGallery.java 1347 2012-02-08 13:36:12Z pengjianbo $
 */
public class BetterGallery extends Gallery {
    private boolean scrollingHorizontally = false;

    public BetterGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BetterGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BetterGallery(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        onTouchEvent(ev);
        return scrollingHorizontally;
    }
    
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if(Math.abs(distanceX)>Math.abs(distanceY) || scrollingHorizontally == true){
            scrollingHorizontally = true;
            super.onScroll(e1, e2, distanceX, distanceY);
        }
        return scrollingHorizontally;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            scrollingHorizontally = false;
            break;
        default:
                break;
        }
        super.onTouchEvent(event);
        return scrollingHorizontally;
    }


}
